<?php

//This is the standard Tamarin library, containing some useful functions

//Generate an alphanumerical ID
function generateKey($length = 32) {
	$key = '';
	$apples = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
	for($i = 0; $i < $length; $i++) {
		$key .= $apples[mt_rand(0, 61)];
	}
	return $key;
}

//Set If Not Set
function sins(&$var, $default) {
	if(!isset($var)) {
		$var = $default;
		return true;
	}
	return false;
}

//Return if set
function ris(&$var, $var2 = null) {
	if(isset($var))
		return $var;
	else
		return $var2;
}

//Is set and equal to
function isaet(&$var,$value) {
	if(isset($var)&&$var==$value)
		return true;
	else
		return false;
}

//Convert the from URL formatting (blee-bla-bloo) to PHP formatting (bleeBlaBloo)
function urlToPhp($url) {
	$letters = str_split($url);
	$capitalize = false;
	foreach($letters as $key => $letter) {
		if($capitalize) {
			$letters[$key] = strtoupper($letter);
			$capitalize = false;
		}
		if($letter=='-') {
			unset($letters[$key]);
			$capitalize = true;
		}
	}
	$php = implode($letters);
	return str_replace('-','',$php);
}

function phpToUrl($php) {
	$split = preg_split('/([A-Z])/', $php, -1, PREG_SPLIT_DELIM_CAPTURE);
	$return = '';
	foreach($split as $key => $value) {
		if($key%2==0) {
			$return .= $value;
		} else {
			$return .= '-'.strtolower($value);
		}
	}
	return $return;
}

function slugify($str) {
	$str = strtolower($str);
	$str = preg_replace('/[^a-z0-9\-]/', '', $str);
	$str = preg_replace('/ +/', '-', $str);
	return $str;
}

?>
